﻿using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Common.Data;
using QDT.Core.Assignments;
using QDT.Core.Deals;
using QDT.Core.Members;

namespace QDT.Web.Admin.Controllers
{
    public class AssignmentController : BaseController
    {

        public ActionResult Index(int? page, GridSortOptions sort, int? assignmentId, string username, int? assignmentStatus)
        {
            User user = null;

            if (!string.IsNullOrWhiteSpace(username))
            {
                user = DbProvider.D<User>().FirstOrDefault(x => x.UserName == username);
            }

            var filter = PredicateExtensionses.True<Assignment>();

            if (assignmentId != null && assignmentId != 0) filter = filter.And(x => x.ID == assignmentId);

            if (user != null) filter = filter.And(x => x.UserId == user.ID);

            if (assignmentStatus.HasValue) filter = filter.And(x => x.InnerStatus == assignmentStatus);

            const int pageSize = 20;

            var pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;

            var deals = DbProvider.D<Assignment>().Where(filter.And(x => !x.IsDelete)).OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            return View(deals.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult BidList(int? page, GridSortOptions sort, int? assignmentId, string username, DateTime? createTime)
        {

            var filter = PredicateExtensionses.True<Credit>();

            if (assignmentId != null && assignmentId != 0) filter = filter.And(x => x.AssignmentID == assignmentId);

            if (!string.IsNullOrWhiteSpace(username)) filter = filter.And(x => x.UserName.Contains(username));

            if (createTime.HasValue)
            {
                var startTime = ((DateTime)createTime).Date;
                var emdTime = startTime.AddDays(1);
                filter = filter.And(x => x.CreateTime >= startTime && x.CreateTime < emdTime);
            }

            const int pageSize = 20;

            var pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;

            var sources = DbProvider.D<Credit>().Where(filter.And(x => x.AssignmentID != 0)).OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            return View(sources.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult ChangeStatus(int id)
        {
            var assign = DbProvider.GetByID<Assignment>(id);
            if (assign == null) return Error("错误");
            if ((DateTime.Now.Date - assign.CreateTime.Date).Days >= 3)
            {
                assign.InnerStatus = (int) AssignmentStatus.Cancel;
                DbProvider.SaveChanges();
                return Success("操作成功");
            }
            return Error("操作失败，时间不满足条件");
        }

        public ActionResult ShowDetail(int id)
        {
            var assign = DbProvider.GetByID<Assignment>(id);
            var credits = DbProvider.D<Credit>().Where(x => x.AssignmentID == id).ToList().Where(x => x.IsEffect).ToList();
            ViewData["Assignment"] = assign;

            return PartialView(credits);
        }

    }
}
